package cn.raven.org.service.impl;

import cn.raven.basic.service.impl.BaseServiceImpl;
import cn.raven.basic.utils.PageList;
import cn.raven.org.domain.Department;
import cn.raven.org.mapper.DepartmentMapper;
import cn.raven.org.query.DepartmentQuery;
import cn.raven.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**

* @Description: 部门管理业务层
* @Author: king
* @Version: 1.0
* @Date:  2022/6/21 17:51
  */
@Service
//只读事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    
    /** 
    * @Title: add
    * @Description: 新增
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/21 17:09  
    * @Parameters: [department]
    * @Return void
      */
    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
        String dirPath = "";
        //先在mapper.xml中返回自增id,在判断他有没有上级部门
        if (department.getParent().getId() == null){
            dirPath = "/"+department.getId();
        }else {
            Long id = department.getParent().getId();
            Department parentDept = departmentMapper.findOne(id);
            if(parentDept != null) {
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);

    }

    /**
    * @Title: update
    * @Description: 更新
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/21 17:43
    * @Parameters: [department]
    * @Return void
      */
    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent() ==null){
            dirPath = "/"+department.getDirPath();
        }else {
            Long id = department.getParent().getId();
            Department parentDept = departmentMapper.findOne(id);
            if(parentDept != null) {
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);

        List<Department> deptAll = departmentMapper.findAll();
        for (Department dept : deptAll){
            if (department.getId() == dept.getParentId()){
                dirPath =department.getDirPath()+"/"+ dept.getId();
                dept.setDirPath(dirPath);
                dept.setParentId(department.getId());
                departmentMapper.updateTwo(dept);
            }
        }
    }

    
    /** 
    * @Title: tree
    * @Description: 上级部门树数据
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/25 15:08
    * @Parameters: []
    * @Return java.util.List<cn.raven.org.domain.Department>
      */
    @Override
    public List<Department> tree() {
        //获取所有数据
        List<Department> depts = departmentMapper.findAll();
        //创建一个装最终结果的集合
        List<Department> deptList =  new ArrayList<Department>();
        //为了解决时间复杂度，转为map
        /*  转换为map集合，stream模式
 Map<Long, Department> deptMap = depts.stream().collect(Collectors.toMap(Department::getId, item -> item));
       */
        Map<Long, Department> deptMap = new HashMap<>();
        for(Department department : depts){
            deptMap.put(department.getId(),department);
        }
        //先把一级数据找出来
        for (Department dept : depts){
            if (dept.getParentId() == null){
                deptList.add(dept);
            }else {
                Department department = deptMap.get(dept.getParentId());
                if (department != null){
                    department.getChildren().add(dept);
                }
            }
        }

        return deptList;
    }
}
